﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Kooboo.HtmlFunc;
using Kooboo.HtmlFunc.Providers;
using Kooboo.CMS.SiteConverter.Providers;

namespace Kooboo.CMS.SiteConverter.Parser.Razor
{
    public class PositionParser : ParserBase, IParser<IPosition>
    {
        public static void ReplaceInner(IPosition pos, HtmlNode inner)
        {
            NodeExtension.ReplaceInner(pos.StartNode, pos.EndNode, inner);

            if (ParserSetting.RemoveFuncMarkers)
            {
                if (pos.StartNode == pos.EndNode)
                {
                    //pos.Attribute.Remove();
                }
                else
                {
                    pos.EndNode.RemoveWithPrevWhitespace();
                    pos.StartNode.ParentNode.RemoveChild(pos.StartNode);
                }
            }
        }

        public static void FillResult(IPosition obj, IParseResult result)
        {
            result.Layout.Positions.Add(new Kooboo.CMS.Sites.Models.LayoutPosition()
            {
                ID = obj.Name
            });
        }

        #region IParser<IPosition> Members

        public void Parse(IPosition obj)
        {
            var markup = string.Format("@Html.FrontHtml().Position(\"{0}\")", obj.Name);
            var markupNode = NodeExtension.CreateNode(markup);
            ReplaceInner(obj, markupNode);

            // result
            FillResult(obj, this.Context.Result);
        }

        #endregion
    }
}
